<html>
<head><meta charset="utf-8"><title>Why do closure have a DefId · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html">Why do closure have a DefId</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199681597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199681597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199681597">(Jun 03 2020 at 21:51)</a>:</h4>
<p>Hello!<br>
This is rather a broad question I suppose but during name resolution closures get assigned a <code>DefId</code> whereas they are never the owners of any <code>HirId</code>. All the children hir node of a closure (and the closure itself actually) have the enclosing fn as <code>HirId</code> owner.<br>
This behavior is relied upon in a lot of different places in the compiler actually: all  nodes in a function are indexed using their <code>local_id</code> against a same unique parent owner but sometime the <code>DefId</code> of the closure is accessed for various checks.</p>



<a name="199681757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199681757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199681757">(Jun 03 2020 at 21:53)</a>:</h4>
<p>This is actually why internally the compiler has to maintain lookup tables to go through <code>DefId</code> -&gt; <code>HirId</code> and <code>HirId</code> -&gt; <code>DefId</code></p>



<a name="199687923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199687923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199687923">(Jun 03 2020 at 23:03)</a>:</h4>
<p>IIUC we need a defid because later stages of the compiler primarily use DefIds for stuff. In particular, the closure desugars to a struct that implements the <code>Fn*</code> traits. In order to do stuff with that closure at later stages in the compilation, that struct needs a defid.</p>



<a name="199687980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199687980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199687980">(Jun 03 2020 at 23:04)</a>:</h4>
<p>(take with a grain of salt, as I'm not really sure of this)</p>



<a name="199697224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199697224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199697224">(Jun 04 2020 at 01:39)</a>:</h4>
<p>I find it strange that the statements inside a closure aren't tied to the DefId of the closure, not that closures have a DefId to begin with. Is there a reason this must be so? Would changing this reduce the number of mappings we have to store somehow?</p>



<a name="199697301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199697301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199697301">(Jun 04 2020 at 01:41)</a>:</h4>
<p>(Seems like a good candidate for a major change proposal)</p>



<a name="199713627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199713627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199713627">(Jun 04 2020 at 07:35)</a>:</h4>
<p>I tried to change it and make the DefId a real owner during lowering, and it breaks tons of stuff on later compilation stages because there are a lot of list of local id only that all refers to the same owner, especially in region checking. C</p>



<a name="199713732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199713732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199713732">(Jun 04 2020 at 07:37)</a>:</h4>
<p>and the fix is not just to store list of HirId instead. Some algorithms need to be "fixed" for that new layout of hir ids</p>



<a name="199772741"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199772741" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199772741">(Jun 04 2020 at 16:24)</a>:</h4>
<p><span class="user-mention silent" data-user-id="281572">marmeladema</span> <a href="#narrow/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId/near/199713627">said</a>:</p>
<blockquote>
<p>I tried to change it and make the DefId a real owner during lowering, and it breaks tons of stuff on later compilation stages because there are a lot of list of local id only that all refers to the same owner, especially in region checking. C</p>
</blockquote>
<p>Obviously changing it would be quite a bit of work, and this is a practical reason not to make this change, but is there a fundamental reason that statements inside a closure aren't tied to the <code>DefId</code> of a closure?</p>



<a name="199789294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199789294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199789294">(Jun 04 2020 at 18:31)</a>:</h4>
<p>I cannot really answer that question but some part like region checking uses that behavior<br>
maybe <span class="user-mention" data-user-id="116009">@nikomatsakis</span> knows a bit more?</p>



<a name="199791718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199791718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mark-i-m <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199791718">(Jun 04 2020 at 18:50)</a>:</h4>
<p>You might be able to find out more on the wg-rfc-2229 channel, since iiuc, they deal with closures and borrow checking as their bread and butter</p>



<a name="199805784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199805784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199805784">(Jun 04 2020 at 20:41)</a>:</h4>
<p><span class="user-mention silent" data-user-id="119009">eddyb</span> suggests that <a href="https://github.com/rust-lang/rust/pull/63849#issuecomment-537270464">this would indeed be possible</a>. I do think this kind of change would need to go through MCP though.</p>



<a name="199810693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199810693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199810693">(Jun 04 2020 at 21:28)</a>:</h4>
<p>By the way what does MCP mean?</p>



<a name="199811512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199811512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pachi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199811512">(Jun 04 2020 at 21:36)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span>  Major change proposal. AFAIK, it's just a new way to propose changes that are either big enough to require some process (not just a pull request) or as a previous step to a full RFC</p>



<a name="199811557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199811557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pachi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199811557">(Jun 04 2020 at 21:37)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> <a href="https://forge.rust-lang.org/compiler/mcp.html">https://forge.rust-lang.org/compiler/mcp.html</a></p>



<a name="199811597"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199811597" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199811597">(Jun 04 2020 at 21:37)</a>:</h4>
<p>Thanks i figure it was some kind of process but i did not what the acronym meant</p>



<a name="199811982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199811982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pachi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199811982">(Jun 04 2020 at 21:41)</a>:</h4>
<p>Glad to help, you're doing a great work!</p>



<a name="199933670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199933670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199933670">(Jun 05 2020 at 21:01)</a>:</h4>
<p><span class="user-mention silent" data-user-id="118594">ecstatic-morse</span> <a href="#narrow/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId/near/199772741">said</a>:</p>
<blockquote>
<p>Obviously changing it would be quite a bit of work, and this is a practical reason not to make this change, but is there a fundamental reason that statements inside a closure aren't tied to the <code>DefId</code> of a closure?</p>
</blockquote>
<p>in some sense yes</p>



<a name="199933700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199933700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199933700">(Jun 05 2020 at 21:01)</a>:</h4>
<p>closure bodies are type-checked in one inference context with their parent function</p>



<a name="199933710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199933710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199933710">(Jun 05 2020 at 21:01)</a>:</h4>
<p>you can observe this if you do</p>



<a name="199933794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199933794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199933794">(Jun 05 2020 at 21:02)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">None</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="mi">22</span><span class="p">);</span><span class="w"> </span><span class="c1">// influences the type of `y`</span>

<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="199933799"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199933799" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199933799">(Jun 05 2020 at 21:02)</a>:</h4>
<p>or things like that</p>



<a name="199933819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199933819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199933819">(Jun 05 2020 at 21:02)</a>:</h4>
<p>so the "hir-ids" kind of reflect that</p>



<a name="199933842"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199933842" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199933842">(Jun 05 2020 at 21:02)</a>:</h4>
<p>and this is also why the typeck-tables for closures and their parents etc are all produced "together"</p>



<a name="199933863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199933863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199933863">(Jun 05 2020 at 21:02)</a>:</h4>
<p>not to say it couldn't be changed</p>



<a name="199937419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199937419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199937419">(Jun 05 2020 at 21:42)</a>:</h4>
<p>Thank you for the explaination</p>



<a name="199937463"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199937463" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199937463">(Jun 05 2020 at 21:43)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> would it make sense to not give a <code>DefId</code> to closures?</p>



<a name="199938323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199938323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199938323">(Jun 05 2020 at 21:54)</a>:</h4>
<p>Closures types need to be referred to cross crate (for inlining, opaque types, etc...)</p>



<a name="199938442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199938442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199938442">(Jun 05 2020 at 21:55)</a>:</h4>
<p>Maybe using a <code>DefId</code> + <code>ItemLocalId</code> is possible there, but it sounds like just as much work.</p>



<a name="199983934"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199983934" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199983934">(Jun 06 2020 at 16:57)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId/near/199933819">said</a>:</p>
<blockquote>
<p>so the "hir-ids" kind of reflect that</p>
</blockquote>
<p>It would be ideal if changes to code <em>inside</em> the closure did not invalidate <code>HirId</code>s for subsequent code outside the closure's definition. I kind of doubt that doing this would have any practical benefit for incremental compilation, however. Maybe I'm wrong?</p>



<a name="199987856"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/199987856" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#199987856">(Jun 06 2020 at 18:42)</a>:</h4>
<p>ideal from an incremental point of view that is. Maybe this makes type-checking harder.</p>



<a name="200236395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200236395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200236395">(Jun 09 2020 at 14:30)</a>:</h4>
<p>well, the point is that changes to code inside the closure <em>can</em> affect type-checking for things outside the closure</p>



<a name="200236423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200236423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200236423">(Jun 09 2020 at 14:30)</a>:</h4>
<p>I don't think there would be any practical benefits from trying to separate them</p>



<a name="200246245"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200246245" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200246245">(Jun 09 2020 at 15:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116118">Matthew Jasper</span> <a href="#narrow/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId/near/199938442">said</a>:</p>
<blockquote>
<p>Maybe using a <code>DefId</code> + <code>ItemLocalId</code> is possible there, but it sounds like just as much work.</p>
</blockquote>
<p>That is basically what a <code>HirId</code> is right? If so could we just use an <code>HirId</code> to reference closures and not assign them <code>DefId</code> at all?</p>



<a name="200246696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200246696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200246696">(Jun 09 2020 at 15:32)</a>:</h4>
<p>An <code>HirId</code> is a <code>LocalDefId</code> + <code>ItemLocalId</code>.</p>



<a name="200246820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200246820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200246820">(Jun 09 2020 at 15:32)</a>:</h4>
<p>Oh right sorry :/</p>



<a name="200247442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200247442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200247442">(Jun 09 2020 at 15:36)</a>:</h4>
<p>It kind of side question, but cross crate referencing works using serialized data that we load when analyzing one crate?</p>



<a name="200247503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200247503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200247503">(Jun 09 2020 at 15:36)</a>:</h4>
<p>If so how do we handle serialization of HirId in such contexts? Because we do serialize HirId already</p>



<a name="200249775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200249775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200249775">(Jun 09 2020 at 15:51)</a>:</h4>
<p>Ah I get it now. For example, changing the body of a closure could affect the inferred return type, which would then affect inference in the caller.</p>



<a name="200249920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200249920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200249920">(Jun 09 2020 at 15:52)</a>:</h4>
<p>There's also the interaction between capture and inference</p>



<a name="200250112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250112">(Jun 09 2020 at 15:53)</a>:</h4>
<p>We don't serialize <code>HirId</code>s to crate metadata</p>



<a name="200250305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250305">(Jun 09 2020 at 15:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="225949">Nathan Corbyn</span> <a href="#narrow/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId/near/200249920">said</a>:</p>
<blockquote>
<p>There's also the interaction between capture and inference</p>
</blockquote>
<p>Does that apply in-to-out or just out-to-in?</p>



<a name="200250341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250341">(Jun 09 2020 at 15:54)</a>:</h4>
<p>(sorry, a bit off topic)</p>



<a name="200250363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250363">(Jun 09 2020 at 15:55)</a>:</h4>
<p>Ok! I've just had to deal with <a href="https://github.com/rust-lang/rust/blob/master/src/librustc_metadata/rmeta/mod.rs#L321-L324">https://github.com/rust-lang/rust/blob/master/src/librustc_metadata/rmeta/mod.rs#L321-L324</a> recently</p>



<a name="200250448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250448">(Jun 09 2020 at 15:55)</a>:</h4>
<p>And i thought we were (de)serializing HirId as part of the <code>ModData</code> struct</p>



<a name="200250505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nathan Corbyn <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250505">(Jun 09 2020 at 15:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId/near/199933794">said</a>:</p>
<blockquote>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">None</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="mi">22</span><span class="p">);</span><span class="w"> </span><span class="c1">// influences the type of `y`</span>

<span class="p">}</span><span class="w"></span>
</code></pre></div>


</blockquote>
<p>I just noticed Niko mentioned this already - I think it works both ways</p>



<a name="200250651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250651">(Jun 09 2020 at 15:56)</a>:</h4>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span> do you know what we end up serializing in this case?</p>



<a name="200250652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250652">(Jun 09 2020 at 15:56)</a>:</h4>
<p>No, it serializes <code>Res&lt;HirId&gt;</code>, but they're never <code>Res::Local</code></p>



<a name="200250674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250674">(Jun 09 2020 at 15:56)</a>:</h4>
<p>Ok</p>



<a name="200250988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200250988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200250988">(Jun 09 2020 at 15:58)</a>:</h4>
<p>Data is coming from <a href="https://github.com/marmeladema/rust/blob/e75922246152e3d9a353e41a643025fc7ffb0f67/src/librustc_resolve/imports.rs#L1396">https://github.com/marmeladema/rust/blob/e75922246152e3d9a353e41a643025fc7ffb0f67/src/librustc_resolve/imports.rs#L1396</a> btw</p>



<a name="200251700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/200251700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#200251700">(Jun 09 2020 at 16:02)</a>:</h4>
<p>So TL;DR is it makes sense for closure not be the real owner of its "children" and at the same time it makes sense for them to have a <code>DefId</code>. I guess the hope to make all DefId hir owner is kind if impossible to achieve</p>



<a name="202036878"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202036878" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202036878">(Jun 25 2020 at 23:17)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="119009">@eddyb</span></p>



<a name="202037065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037065">(Jun 25 2020 at 23:21)</a>:</h4>
<p>So yeah I created this thread because I wanted to investigate your ideas of making all LocalDefId hir owner, but at least for closure, it seems difficult</p>



<a name="202037173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037173">(Jun 25 2020 at 23:23)</a>:</h4>
<p>so on the side of making all of them HIR owners, I had envisioned having some sort of enum that's a subset of <code>hir::Node</code> (and have it be called <code>hir::Owner</code>), and have all of the <code>hir::map</code> collection and HIR visitors interact with that to avoid a massive amount of code duplication</p>



<a name="202037335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037335">(Jun 25 2020 at 23:25)</a>:</h4>
<p>specifically for closures, <code>TypeckTables</code> is, I suspect, 99% of the problem. there are two problems I can think of:</p>
<ol>
<li>currently <code>typeck_tables_of</code> on a closure returns the parent's <code>TypeckTables</code>, and <code>TypeckTables</code> is designed around being keyed on <code>ItemLocalId</code></li>
<li>custom visitor doesn't call <code>typeck_tables_of</code> / <code>body_tables</code> / etc. to change <code>TypeckTables</code> when switching between the parent and the closure</li>
</ol>



<a name="202037404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037404">(Jun 25 2020 at 23:26)</a>:</h4>
<p>(small aside, we should probably rename <code>ItemLocalId</code> with <code>IntraOwnerId</code> or something, both because "item" should probably be replaced with "owner" but also "local" is probably unnecessary confusing)</p>



<a name="202037523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037523">(Jun 25 2020 at 23:28)</a>:</h4>
<p>Yeah I think a few struct and methods around HirId should be renamed. About closure, previous people in the thread said closure where not considered their own owner for reasons about type checking outside of the closure</p>



<a name="202037540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037540">(Jun 25 2020 at 23:29)</a>:</h4>
<p>AFAIK it's 100% because of <code>TypeckTables</code>' design</p>



<a name="202037549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037549">(Jun 25 2020 at 23:29)</a>:</h4>
<p>which may have been introduced at the same time as <code>HirId</code>, I forget</p>



<a name="202037574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037574">(Jun 25 2020 at 23:29)</a>:</h4>
<p>all the maps in it are keyed by <code>ItemLocalId</code> and everything shares one <code>LocalDefId</code> (HIR owner, really)</p>



<a name="202037646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037646">(Jun 25 2020 at 23:30)</a>:</h4>
<p>yeah I did try to make closure HirId owner and I had to deal with all those maps of ItemLocalId</p>



<a name="202037691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037691">(Jun 25 2020 at 23:31)</a>:</h4>
<p>but also closures aren't the only one, they're just the biggest case. I found half a dozen more, and I probably still have the abandoned branches somewhere (they were before I realized I needed a <code>hir::Owner</code> enum or similar to avoid massive code duplication)</p>



<a name="202037769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037769">(Jun 25 2020 at 23:32)</a>:</h4>
<p>So why would be the use of that <code>hir::Owner</code> enum?</p>



<a name="202037776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037776">(Jun 25 2020 at 23:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="119009">eddyb</span> <a href="#narrow/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId/near/202037540">said</a>:</p>
<blockquote>
<p>AFAIK it's 100% because of <code>TypeckTables</code>' design</p>
</blockquote>
<p>well, am I lying? not sure. I was getting to the other side of this which is that "item-like" (items, trait items, impl items, and I guess foreign items) were considered special (and still kind of are) in a bunch of places and I have a vendetta against them</p>



<a name="202037804"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037804" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037804">(Jun 25 2020 at 23:32)</a>:</h4>
<p>and closures are not "item-like" so they just didn't get to be HIR owners</p>



<a name="202037830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037830">(Jun 25 2020 at 23:33)</a>:</h4>
<p>not sure which is "more overriding", that or <code>TypeckTables</code>. I guess they conspire to make it harder to make closures owners</p>



<a name="202037845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037845">(Jun 25 2020 at 23:33)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> everywhere you see the same code written thrice for items, impl items, and trait items, it would make that go away. there's a lot of stuff in <code>hir::map</code> especially</p>



<a name="202037921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037921">(Jun 25 2020 at 23:34)</a>:</h4>
<p>it would also allow any visitor to easily handle situations when the <code>HirId</code> owner changes (to avoid being caught by surprise - for whatever reason)</p>



<a name="202037927"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037927" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037927">(Jun 25 2020 at 23:34)</a>:</h4>
<p>anyway back to the 2 problems I stated</p>



<a name="202037943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202037943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202037943">(Jun 25 2020 at 23:35)</a>:</h4>
<p>problem 1. should be easy to deal with, we just create one <code>TypeckTables</code> per closure and have a map inside the parent, so that <code>typeck_tables_of</code> can grab it from there (there's probably gonna be a funny thing where we probably want nested closures to each track their children <code>TypeckTables</code>, instead of flattening it, just to keep it sane)</p>



<a name="202038001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038001">(Jun 25 2020 at 23:36)</a>:</h4>
<p>and the nice thing is that we can do it today (which is something I had thought about then forgot), i.e. we can make it so <code>typeck_tables_of</code> returns different <code>TypeckTables</code> for a closure and its parent (even if they both use the parent as the HIR owner)</p>



<a name="202038043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038043">(Jun 25 2020 at 23:36)</a>:</h4>
<p>which would force us to fix problem 2. without any of the complications arising from making non-"item-likes" HIR owners</p>



<a name="202038079"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038079" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038079">(Jun 25 2020 at 23:37)</a>:</h4>
<p>(the fix is on a case by case basis, but presumably it involves things like e.g. hooking <code>visit_nested_body</code>, or handling <code>ExprKind::Closure</code> specially)</p>



<a name="202038163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038163">(Jun 25 2020 at 23:39)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> does my suggestion for the first step (nest <code>TypeckTables</code> instead of sharing them between closures and their parents, w/o changing anything about HIR owners) make sense? I can do into detail if you want</p>



<a name="202038256"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038256" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038256">(Jun 25 2020 at 23:40)</a>:</h4>
<p>I will try to get familiar with TypeckTables a bit more first. But yeah if you had some time to write that plan into more detail that would be awesome and i'd be glad to help!</p>



<a name="202038269"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038269" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038269">(Jun 25 2020 at 23:40)</a>:</h4>
<p>Right now, i have to logoff to get some sleep first :P</p>



<a name="202038313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038313">(Jun 25 2020 at 23:41)</a>:</h4>
<p>Thank you for all the explanations, let's continue tomorrow if that's ok for you</p>



<a name="202038885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038885">(Jun 25 2020 at 23:50)</a>:</h4>
<p><code>TypeckTables</code> is a poorly named (<a href="https://github.com/rust-lang/rust/pull/72983">https://github.com/rust-lang/rust/pull/72983</a>) grabbag of type-inference/checking results (like types of expression/pattern nodes, <code>DefId</code>s for method calls, what coercions are needed by expressions, etc.)</p>



<a name="202038921"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038921" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038921">(Jun 25 2020 at 23:51)</a>:</h4>
<p>because closures have shared inference with their parent (only closures have this, all other bodies are isolated inference-wise), they use the same <code>TypeckTables</code></p>



<a name="202038975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202038975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202038975">(Jun 25 2020 at 23:52)</a>:</h4>
<p>and this is where <code>typeck_tables_of(closure)</code> just returns <code>typeck_tables_of(parent)</code> <a href="https://github.com/rust-lang/rust/blob/master/src/librustc_typeck/check/mod.rs#L981-L986">https://github.com/rust-lang/rust/blob/master/src/librustc_typeck/check/mod.rs#L981-L986</a></p>



<a name="202039006"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20do%20closure%20have%20a%20DefId/near/202039006" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> eddyb <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20do.20closure.20have.20a.20DefId.html#202039006">(Jun 25 2020 at 23:53)</a>:</h4>
<p>aww, that relies on <code>closure_base_def_id</code>, so maybe what I said about flattening being bad doesn't hold. but maybe there's a <code>closure_parent_def_id</code>?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>